package package1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class TestForFileReaderAndWriter {

	/**
	 * file reading and writing
	 * basie input and output of IO stream 
	 * interaction with user through consolo by chosing which file  to read
	 * @param args
	 * @throws IOException 
	 */
	public static String DESKTOP = "c:\\users\\alvin.wang\\desktop";
	public static void main(String[] args) throws IOException, InterruptedException {
//		fileOutputStreamTest();
//		readFileBasicVersionForEnterMark();
//		printWriterTest();
//		printStreamTest();
//		File a = new File(DESKTOP+"\\h.class");
//		File b = new File("h.class");
//		bufferedInputStreamTest(a, b); //byte buf would not spoil class file
//		bufferedReaderFileReaderTest(a, b); //String buf would spoil class file
//		run(b);
		
		String desktop = getDesktop();
//		File f1 = new File(desktop+"t1\\h1.class");
//		File f2 = new File(desktop+"t2\\h1.class");
		File folder1 = new File(desktop+"t1\\");
		File folder2 = new File(desktop+"t2\\");
		File[]fs = folder1.listFiles();
		List list = new ArrayList();
		
		for(File f:fs){
			if(!f.getName().endsWith("class"))
				continue;
			list.add(f.getName());
		}
		Collections.reverse(list);
		for(Object o:list){
			File f1 = new File(folder1,(String) o);
			File f2 = new File(folder2,(String) o);
			copyInByte(f1,f2);
			pause(1);
		}

//		System.err.println(list);
//		Collections.reverse(list);
//		System.err.println(list);
		
		open(folder2);
//		copyInByte(f1,f2);
//		copyInString(f1,f2);
//		copyInChar(f1,f2);
//		open(f2.getParent());
		clearFileContent("");
	}
	
	
	private static void clearFileContent(String logFile) throws IOException {
		clearFileContent(new File(logFile));
	}



	private static void clearFileContent(File file) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter(file,false));
		bw.write("");
		bw.close();
	}

	
	private static void pause(int i) throws InterruptedException {
		Thread.currentThread().sleep(i*1000);
	}

	private static void copyInChar(File f1, File f2) throws IOException {
		InputStreamReader isr = new InputStreamReader(new FileInputStream(f1));
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f2));
		char[]cbuf = new char[1024];
		int len;
		while((len=isr.read(cbuf))!=-1){
			osw.write(cbuf, 0, len);
			osw.flush();
		}
		osw.close();
		isr.close();
	}

	private static void copyInString(File f1, File f2) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(f1));
		BufferedWriter bw = new BufferedWriter(new FileWriter(f2));
		String buf;
		while((buf = br.readLine())!=null){
			bw.write(buf);
		}
		bw.flush();
		bw.close();
		br.close();
	}

	private static void copyInByte(File f1, File f2) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2));
		byte []buf = new byte[1024];
		int len;
		while((len=bis.read(buf))!=-1){
			bos.write(buf, 0, len);
			bos.flush();
		}
		bos.close();
		bis.close();
		open(f2.getParent());
		
	}

	private static String getDesktop() throws IOException {
		return System.getenv().get("USERPROFILE")+"\\desktop\\";
	}

	private static String open(File f) throws IOException {
		java.awt.Desktop.getDesktop().open(f);
		return f.getAbsolutePath();
	}

	private static String open(String f) throws IOException {
		open(new File(f));
		return f;
	}

	
	public static void fileOutputStreamTest() throws IOException {
		FileOutputStream fos = new FileOutputStream("aa.txt");
		fos.write("abcdefg".getBytes());
		fos.close();
		openCurrentFolder();
	}
	public static void openCurrentFolder() throws IOException {
		String aa = new File(new File("a").getAbsolutePath()).getParent();
		open(aa);
	}

	private static void run(String s) throws IOException {
		Runtime.getRuntime().exec(s);
	}


	@SuppressWarnings("unused")
	private static void run(File b) throws IOException {
		String cmd = "java "+b.getName().replace(".class", "");
		System.err.println("cmd:"+cmd);
		run(cmd);
	}

	@SuppressWarnings("unused")
	private static void bufferedInputStreamTest(File a, File b) throws FileNotFoundException, IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(a));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(b));
		byte [] buf = new byte[1024];
		int pos;
		while((pos=bis.read(buf))!=-1){
			bos.write(buf, 0, pos);
		}
		bos.flush();bis.close();bos.close();
	}

	@SuppressWarnings("unused")
	private static void bufferedReaderFileReaderTest(File a, File b) throws FileNotFoundException, IOException {
		BufferedReader br = new BufferedReader(new FileReader(a));
		BufferedWriter bw = new BufferedWriter(new FileWriter(b));
		String buf;
		while((buf=br.readLine())!=null){
			bw.write(buf);
			bw.newLine();
		}
		br.close();bw.close();
		open(b.getParent());
	}

	@SuppressWarnings("unused")
	private static void writeToFile(String fileName, String str) throws IOException {
		File f = new File(fileName);
		System.err.println("writeToFilepath:"+fileName);
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		bw.write(str);
		bw.close();
	}
	@SuppressWarnings("unused")
	private static void readKey2() throws IOException {
		InputStream in = System.in;
		int ch = 0;
		StringBuffer sb = new StringBuffer();
		while((ch=in.read())!=-1){
			if(ch==13)// \r
				continue;
			if(ch==10)// \n
			{
				if(sb.toString().equalsIgnoreCase("stop"))
					break;
				System.err.println(sb);
				//sb = new StringBuffer();
				sb.delete(0, sb.length());
				continue;
			}	
			sb.append(String.valueOf(((char)ch)).toUpperCase());
		}
		System.err.println("over");
	}

	@SuppressWarnings("unused")
	private static void readKey() throws IOException {
		InputStream in = System.in;
		int ch1 = in.read();
		System.err.println(ch1);//number 1
		int ch2 = in.read();
		System.err.println(ch2);//'enter' takes 2 byte position \r=13
		int ch3 = in.read();
		System.err.println(ch3);//'enter' takes 2 byte position \n=10
		int ch4 = in.read();
		System.err.println(ch4);// if input 3 bytes, but you define to read 4 bytes, JVM waits for you to input the 4th byte
		System.err.println("over");
		
		in.close();
		InputStream in2 = System.in;
		int ch5 = in2.read();
		System.err.println(ch5);
	}
	@SuppressWarnings("unused")
	private static void print(byte[] buf, int pos) {
		for(int i=0;i<=10;i++){
			System.err.println("pos:"+pos);
			System.err.print(buf[i]);
		}
	}
	
	@SuppressWarnings({ "unused", "resource" })
	private static void readkey_5() throws IOException {
		BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
		BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b.txt")));
		String line;
		while((line=bufr.readLine())!=null){
			bufw.write(line);
			bufw.newLine();
			bufw.flush();
		}
	}
	@SuppressWarnings({ "unused", "resource" })
	private static void readkey_4() throws IOException {
		BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
		BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
		String line;
		while((line=bufr.readLine())!=null){
			bufw.write(line);
			bufw.newLine();
			bufw.flush();
		}
	}

	@SuppressWarnings({ "unused", "resource" })
	private static void readkey_3() throws IOException {
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt")));
		String line;
		while((line=bufr.readLine())!=null){
			if(line.equals("stop"))
				break;
			bufw.write(line);
			bufw.newLine();
			bufw.flush();
		}
	}

	@SuppressWarnings("unused")
	private static void readkey_2() throws IOException {
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
		String line;
		while((line=bufr.readLine())!=null){
			if(line.equals("stop"))
				break;
			bufw.write(line);
			bufw.flush();
		}
	}

	@SuppressWarnings("unused")
	private static void readkey_1() throws IOException {
		InputStream is = System.in;
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		OutputStream os = System.out;
		OutputStreamWriter osw = new OutputStreamWriter(os);
		BufferedWriter bufw = new BufferedWriter(osw);
		String line;
		while((line=br.readLine())!=null){
			if(line.equals("stop"))
				break;
//			osw.write(line.toUpperCase());
//			osw.flush();
			bufw.write(line);
			bufw.flush();
		}
		System.err.println("over");
	}
	@SuppressWarnings("unused")
	private static void readkey_0() throws IOException {
		InputStream is = System.in;
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		OutputStream os = System.out;
		int ch;
		while((ch=br.read())!=-1){
			os.write(ch);
		}
		System.err.println("over");
	}
	
	@SuppressWarnings("unused")
	private static void readFileBasicVersionForEnterMark() throws IOException {
		FileReader fr = null;
		FileWriter fw = null;
		BufferedReader bf = null;
		BufferedWriter bw= null;
		try {
			fr = new FileReader("read.txt");
			fw = new FileWriter("write.txt",false);//if added true, when it goes on from the end instead override from the beginning 
			bf = new BufferedReader(fr);
			bw = new BufferedWriter(fw);
			StringBuffer bs = new StringBuffer();
			int index = 0;
			while((index = fr.read())!=-1){
				if(String.valueOf((char)index).equals("\n")){
					System.err.println("nextline N  "+index);
					index = 'n';
				}
				if(String.valueOf((char)index).equals("\r")){// \r first and then \n
					System.err.println("nextline R   "+index);
					index = 'r';
				}
				
				bs.append((char)index);
				fw.write(index);
				fw.flush();
				System.err.println("*****:"+bs);
			}
			System.err.println("bs:"+bs);
			
		} catch (Exception e) {
			try {
				fr.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				fw.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		open(new File("write.txt").getAbsolutePath());
	}
	@SuppressWarnings({ "unused", "resource" })
	private static void printWriterTest() throws IOException {
		BufferedReader br;
		br = new BufferedReader(new InputStreamReader(System.in));
		Scanner s = new Scanner(System.in);
		
		PrintWriter pw = new PrintWriter(new FileWriter("printwritertest.txt"),true);
//		pw = new PrintWriter(System.out);
		String line = null;
		while((line=s.next())!=null){
			if(line.equals("exit"))
				break;
			pw.write(line+"\r\n");
		}
		pw.close();
//		while((line=br.readLine())!="a"){
//			pw.write(line);
//		}
//		pw.write("nihao");
//		pw.write("\r\n");
//		pw.write(111);// if txt shows 'o', we would know 111 stands for a char type
//		pw.print(true);
//		pw.print("abbaa");
//		pw.flush();
//		pw.close();
		System.err.println("success");
	}
	@SuppressWarnings("unused")
	private static void printStreamTest() throws IOException {
		PrintStream ps = new PrintStream("ps.txt");
		ps.append("a");
		ps.append("b");
		ps.print("hello");
		ps.print("\t\r\n");
		ps.write(109);
		ps.flush();
		ps.close();
	}

}
